package com.totem.customer.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.totem.base.CacheHolder;
import com.totem.base.constants.SecurityComponets;
import com.totem.base.model.PageVO;
import com.totem.base.model.StateEnum;
import com.totem.base.util.RandomValueUtils;
import com.totem.customer.mapper.TCustomerMapper;
import com.totem.customer.model.TCustomers;
import com.totem.customer.serivce.ITCustomerService;
import com.totem.customer.vo.CustomerPageVO;
import com.totem.customer.vo.CustomerVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TCustomerServiceImpl extends ServiceImpl<TCustomerMapper, TCustomers> implements ITCustomerService {
    private Argon2PasswordEncoder argon2Encoder = SecurityComponets.ARGON2_PASSWORD_ENCODER;
    @Autowired
    private TCustomerMapper tCustomerMapper;
    @Autowired
    private CacheHolder cacheHolder;

    @Override
    public boolean createRandomCustomer(int count) {
        if(count<=0) return true;

        List<TCustomers> list = new ArrayList<>();
        for(int i=0; i<count; i++){
            TCustomers tCustomers = buildRandomCustomer();
            list.add(tCustomers);
        }
        saveBatch(list);
        return true;
    }

    @Override
    public TCustomers findByNickname(String nickname) {
        LambdaQueryWrapper<TCustomers> findWrapper = new LambdaQueryWrapper<>();
        findWrapper.eq(TCustomers::getNickname, nickname);
        TCustomers tCustomers = tCustomerMapper.selectOne(findWrapper);
        return tCustomers;
    }

    @Override
    public boolean createCustomer(TCustomers tCustomers) {
        String inviteCode = RandomValueUtils.capitalNumber(6);
        LambdaQueryWrapper<TCustomers> findWrapper = new LambdaQueryWrapper<>();
        findWrapper.eq(TCustomers::getInviteCode, inviteCode);
        boolean exists = tCustomerMapper.exists(findWrapper);
        while(exists){
            findWrapper.clear();
            inviteCode = RandomValueUtils.capitalNumber(6);
            findWrapper.eq(TCustomers::getInviteCode, inviteCode);
            exists = tCustomerMapper.exists(findWrapper);
        }

        tCustomers.setInviteCode(inviteCode);
        int effect = tCustomerMapper.insert(tCustomers);
        return effect>0;
    }

    @Override
    public String defaultPassword() {
        return argon2Encoder.encode("123456");
    }

    @Override
    public PageVO<CustomerVO> page(CustomerPageVO vo) {
        Page<TCustomers> page = new Page<>();
        page.setCurrent(vo.getCurrent());
        page.setSize(vo.getPageSize());

        //page.setOrders(vo.getOrders());
        LambdaQueryWrapper<TCustomers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(vo.getNickname()), TCustomers::getNickname, vo.getNickname())
                .eq(StringUtils.isNotBlank(vo.getCountryCode()), TCustomers::getCountryCode, vo.getCountryCode())
                .eq(StringUtils.isNotBlank(vo.getMobile()), TCustomers::getMobile, vo.getMobile())
                .eq(StringUtils.isNotBlank(vo.getEmail()), TCustomers::getEmail, vo.getEmail())
                .eq(TCustomers::getState, StateEnum.NORMAL.getCode())
                /*
                .between(vo.()!=null && vo.getBirthdayEnd()!=null,
                TCustomers::getCreateTime, vo.getBirthdayStart(), vo.getBirthdayEnd())*/
        ;
        List<CustomerVO> voList = super.page(page, queryWrapper).getRecords().stream().map(entity->{
            CustomerVO info = new CustomerVO();
            BeanUtils.copyProperties(entity, info);
            return info;
        }).collect(Collectors.toList());

        PageVO<CustomerVO> list = new PageVO<>();
        BeanUtils.copyProperties(vo, list);
        list.setList(voList);
        list.setTotal(page.getTotal());
        return list;
    }

    private TCustomers buildRandomCustomer(){
        TCustomers tCustomers = TCustomers.builder()
                .nickname(randomName())
                .password(argon2Encoder.encode("123456"))
                .inviteCode(randomInviteCode())
                .mobile(randomMobile())
                .countryCode("86")
                .email(randomEmail())
                .source("user")
                .build();
        return tCustomers;
    }

    private String randomInviteCode(){
        return RandomValueUtils.capitalNumber(6);
    }

    private String randomName(){
        return "匿名用户-"+ RandomValueUtils.getStr(10, 11);
    }

    private String randomEmail(){
        return RandomValueUtils.getEmail(5, 12);
    }

    private String randomMobile(){
        return RandomValueUtils.getTel();
    }
}
